package com.arellomobile.android.push;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import com.arellomobile.android.push.data.PushZoneLocation;
import com.arellomobile.android.push.utils.WorkerTask;
import com.arellomobile.android.push.utils.executor.ExecutorHelper;
import com.google.android.gcm.GCMRegistrar;

public class GeoLocationService extends Service
{
  private static final int TEN_METERS = 10;
  private static final int TEN_SECONDS = 10000;
  private static final int TWO_MINUTES = 120000;
  private boolean mIfUpdating = false;
  private final LocationListener mListener = new LocationListener()
  {
    public void onLocationChanged(Location paramAnonymousLocation)
    {
      GeoLocationService.this.updateLocation(paramAnonymousLocation);
    }

    public void onProviderDisabled(String paramAnonymousString)
    {
    }

    public void onProviderEnabled(String paramAnonymousString)
    {
    }

    public void onStatusChanged(String paramAnonymousString, int paramAnonymousInt, Bundle paramAnonymousBundle)
    {
    }
  };
  private LocationManager mLocationManager;
  private long mMinDistance;
  private Location mOldLocation;
  private final Object mSyncObject = new Object();
  private PowerManager.WakeLock mWakeLock;

  private void initService()
  {
    this.mWakeLock = ((PowerManager)getSystemService("power")).newWakeLock(1, getClass().getName());
    this.mWakeLock.acquire();
    this.mLocationManager = ((LocationManager)getSystemService("location"));
    Location localLocation2;
    do
    {
      Location localLocation1;
      synchronized (this.mSyncObject)
      {
        this.mLocationManager.removeUpdates(this.mListener);
        localLocation1 = requestUpdatesFromProvider("gps", 10L);
        localLocation2 = requestUpdatesFromProvider("network", 10L);
        if ((localLocation1 != null) && (localLocation2 != null))
        {
          updateLocation(getBetterLocation(localLocation1, localLocation2));
          return;
        }
      }
      if (localLocation1 != null)
      {
        updateLocation(localLocation1);
        return;
      }
    }
    while (localLocation2 == null);
    updateLocation(localLocation2);
  }

  private boolean isSameProvider(String paramString1, String paramString2)
  {
    if (paramString1 == null)
      return paramString2 == null;
    return paramString1.equals(paramString2);
  }

  private Location requestUpdatesFromProvider(String paramString, long paramLong)
  {
    this.mMinDistance = paramLong;
    try
    {
      boolean bool = this.mLocationManager.isProviderEnabled(paramString);
      Object localObject = null;
      if (bool)
      {
        this.mLocationManager.requestLocationUpdates(paramString, 10000L, (float)paramLong, this.mListener);
        Location localLocation = this.mLocationManager.getLastKnownLocation(paramString);
        localObject = localLocation;
      }
      return localObject;
    }
    catch (Exception localException)
    {
      Log.e(getClass().getName(), "Check ACCESS_FINE_LOCATION permission", localException);
    }
    return null;
  }

  private void updateLocation(final Location paramLocation)
  {
    synchronized (this.mSyncObject)
    {
      if ((!this.mIfUpdating) && (GCMRegistrar.isRegisteredOnServer(this)))
      {
        if ((this.mOldLocation != null) && (paramLocation.distanceTo(this.mOldLocation) < (float)this.mMinDistance))
          return;
        this.mOldLocation = paramLocation;
        this.mLocationManager.removeUpdates(this.mListener);
        this.mIfUpdating = true;
        ExecutorHelper.executeAsyncTask(new WorkerTask(this)
        {
          protected PushZoneLocation mZoneLocation;

          protected void doWork(Context paramAnonymousContext)
            throws Exception
          {
            this.mZoneLocation = DeviceFeature2_5.getNearestZone(paramAnonymousContext, paramLocation);
          }

          protected void onPostExecute(Void paramAnonymousVoid)
          {
            super.onPostExecute(paramAnonymousVoid);
            Object localObject1 = GeoLocationService.this.mSyncObject;
            long l1 = 0L;
            try
            {
              if (this.mZoneLocation != null)
                l1 = this.mZoneLocation.getDistanceTo();
              long l2 = Math.max(10L, l1);
              GeoLocationService.this.requestUpdatesFromProvider("gps", l2);
              GeoLocationService.this.requestUpdatesFromProvider("network", l2);
              GeoLocationService.this.mIfUpdating = false;
              return;
            }
            finally
            {
            }
          }
        });
      }
      return;
    }
  }

  protected Location getBetterLocation(Location paramLocation1, Location paramLocation2)
  {
    if (paramLocation2 == null);
    label38: int k;
    label65: label71: label75: 
    while (true)
    {
      return paramLocation1;
      long l = paramLocation1.getTime() - paramLocation2.getTime();
      int i;
      int j;
      if (l > 120000L)
      {
        i = 1;
        if (l >= -120000L)
          break label65;
        j = 1;
        if (l <= 0L)
          break label71;
      }
      for (k = 1; ; k = 0)
      {
        if (i != 0)
          break label75;
        if (j == 0)
          break label77;
        return paramLocation2;
        i = 0;
        break;
        j = 0;
        break label38;
      }
    }
    label77: int m = (int)(paramLocation1.getAccuracy() - paramLocation2.getAccuracy());
    int n;
    label97: int i1;
    if (m > 0)
    {
      n = 1;
      if (m >= 0)
        break label168;
      i1 = 1;
      label105: if (m <= 200)
        break label174;
    }
    label168: label174: for (int i2 = 1; ; i2 = 0)
    {
      boolean bool = isSameProvider(paramLocation1.getProvider(), paramLocation2.getProvider());
      if ((i1 != 0) || ((k != 0) && (n == 0)) || ((k != 0) && (i2 == 0) && (bool)))
        break;
      return paramLocation2;
      n = 0;
      break label97;
      i1 = 0;
      break label105;
    }
  }

  public IBinder onBind(Intent paramIntent)
  {
    return null;
  }

  public void onDestroy()
  {
    super.onDestroy();
    this.mLocationManager.removeUpdates(this.mListener);
    this.mWakeLock.release();
    this.mWakeLock = null;
    this.mLocationManager = null;
  }

  public int onStartCommand(Intent paramIntent, int paramInt1, int paramInt2)
  {
    super.onStartCommand(paramIntent, paramInt1, paramInt2);
    initService();
    return 1;
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.arellomobile.android.push.GeoLocationService
 * JD-Core Version:    0.6.2
 */